package com.reader.cms.interceptor;


import com.reader.cms.annotaion.MemberCheck;
import com.reader.core.utils.CommonVariable;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * add by jiahaikun for 进行会员过期检查拦截
 */

@Component
public class MemberCheckInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MemberCheck annotation;
        if(handler instanceof HandlerMethod) {
            annotation = ((HandlerMethod) handler).getMethodAnnotation(MemberCheck.class);
        }else{
            return true;
        }

       //有MemberCheck的方法则进行会员过期验
        if(annotation != null){
            String loginID = request.getHeader(CommonVariable.LOGIN_ID);
          /*  int remainDays= memberCheckService.getMemberRemainDays(loginID);
            if(remainDays<=0){
                throw new CustomException("ERR_12015");
            }*/
        }

        return true;
    }

    @Override
    public void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {

    }
    @Override
    public void afterCompletion(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }

    /**
     * 跨域正常返回到前端，避免前端异常  //20170926 add by jiahaikun
     * @param request
     * @param response
     */
    private  void setAccessControl(HttpServletRequest request, HttpServletResponse response){
        response.addHeader("Access-Control-Allow-origin","*");
        response.addHeader("Access-Control-Allow-Methods","GET, POST, DELETE, PUT");
        response.addHeader("Access-Control-Allow-Credentials","true");
    }

}
